package com.cap.dreamcircle.View.Adapter.Item;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Common.RecyclerView.BaseItem;
import com.cap.dreamcircle.Model.Bean.SelfSimpleDreamBean;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.DisplayUtil;
import com.cap.dreamcircle.View.DreamDetailActivity;


/**
 * Created by liucxu on 2017/8/10.
 */

public class SelfSimpleDreamItem extends BaseItem<SelfSimpleDreamBean> {

    SelfSimpleDreamBean resultsBean;
    private TextView tv_dream_content;
    private TextView tv_friend_dream_time;
    private ImageView imv_dream_share;
    private ImageView imv_dream_like;
    private ImageView imv_dream_comment;
    private RelativeLayout layout_audio;
    private ImageView imv_playing_status_audio;
    private TextView tv_length_audio;

    public SelfSimpleDreamItem(ViewGroup parent) {
        super(parent);
    }

    @Override
    public int getRes() {
        return R.layout.adapter_self_simple_dream;
    }

    @Override
    public void onCreateView(View rootView) {
        tv_dream_content = (TextView) rootView.findViewById(R.id.tv_dream_content);
        tv_friend_dream_time = (TextView) rootView.findViewById(R.id.tv_friend_dream_time);
        imv_dream_share = (ImageView) rootView.findViewById(R.id.imv_dream_share);
        imv_dream_like = (ImageView) rootView.findViewById(R.id.imv_dream_like);
        imv_dream_comment = (ImageView) rootView.findViewById(R.id.imv_dream_comment);
        layout_audio = (RelativeLayout) rootView.findViewById(R.id.layout_audio);
        imv_playing_status_audio = (ImageView) rootView.findViewById(R.id.imv_playing_status_audio);
        tv_length_audio = (TextView) rootView.findViewById(R.id.tv_length_audio);
    }

    @Override
    public void onBindView(SelfSimpleDreamBean selfSimpleDreamBean) {
        this.resultsBean = selfSimpleDreamBean;
        if (TextUtils.isEmpty(resultsBean.getDreamText())) {
            tv_dream_content.setVisibility(View.GONE);
        } else {
            tv_dream_content.setVisibility(View.VISIBLE);
            tv_dream_content.setText(resultsBean.getDreamText());
        }
        if (TextUtils.isEmpty(resultsBean.getDreamVoiceUrl())) {
            layout_audio.setVisibility(View.GONE);
        } else {
            layout_audio.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(layout_audio.getLayoutParams());
            layoutParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
            int min_width = DisplayUtil.dip2px(App.GetInstance(), 128);
            int max_width=DisplayUtil.GetDisplayWidth()-DisplayUtil.dip2px(App.GetInstance(), 36);
            int width=min_width+((int)(resultsBean.getAudio_length()*(max_width-min_width)/180));
            layout_audio.getLayoutParams().width=width;
            layout_audio.requestLayout();
        }
        tv_friend_dream_time.setText(resultsBean.getCreateTime().substring(0,10));
        tv_length_audio.setText(resultsBean.getAudio_length()+"'");
    }

    @Override
    public void onClick(View v) {
        Intent intent =new Intent(getContext(), DreamDetailActivity.class);
        intent.putExtra("dreamId",resultsBean.getId());
        ActivityCompat.startActivity((Activity) getContext(), intent, null);
    }
}
